package cn.jingyuan.owl.model.paging;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 分页工具
 */
public class PageUtils {

    //region build Page

    public static <E extends Serializable> Page<?> build(int page, int pageSize, int totalCount, List<E> records) {
        return new Page<>(page, pageSize, totalCount, records);
    }

    public static <E extends Serializable> Page<?> build(int page, int pageSize, int totalCount, List<E> records, Map<String, E> countResultMap) {
        return new Page<>(page, pageSize, totalCount, records, countResultMap);
    }

    //endregion

    //region build PageQueryModel

    public static <Q extends Serializable> PageQueryModel<?> build(Q queryModel) {
        PageQueryModel<Q> bean = new PageQueryModel<>();
        bean.setParams(queryModel);
        return bean;
    }

    public static <Q extends Serializable> PageQueryModel<?> build(PageModel pageModel, Q queryModel) {
        PageQueryModel<Q> bean = new PageQueryModel<>();
        bean.setParams(queryModel);

        if (null != pageModel) {
            bean.setPage(pageModel.getPage());
            bean.setPageSize(pageModel.getPageSize());
        }

        return bean;
    }

    public static <Q extends Serializable> PageQueryModel<?> build(int page, int pageSize, Q queryModel) {
        PageQueryModel<Q> bean = new PageQueryModel<>();
        bean.setParams(queryModel);

        if (0 < page) {
            bean.setPage(page);
        }

        if (1 <= pageSize) {
            bean.setPageSize(pageSize);
        }

        return bean;
    }

    //endregion

}
